package com.coocaa.ops.api.v1.content.provider;

import com.coocaa.ops.api.common.validator.PolicyValidator;
import com.coocaa.ops.api.v1.content.cache.IPanelInfoCache;
import com.coocaa.ops.api.v1.content.model.internal.PanelContentInfo;
import com.coocaa.ops.api.v1.content.model.internal.PanelSimpleInfo;
import com.coocaa.ops.api.v1.content.model.output.normal.Container;
import com.coocaa.ops.api.v1.content.model.query.PanelQueryModel;
import com.coocaa.ops.api.v1.content.service.internal.DmpTabPanelService;
import com.coocaa.ops.api.v1.content.util.TabContentResultUtil;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.panel.constant.PanelTypeConstants;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author tangminghong
 * @date 2020-06-28
 */
@Service
public class PanelProviderImpl implements IPanelProvider {

    @Autowired
    private IPanelInfoCache panelInfoCache;

    @Autowired
    private DmpTabPanelService dmpTabPanelService;

    @Override
    public List<Container> getPanelContents(List<PanelSimpleInfo> panelSimpleList, PanelQueryModel queryModel) {
        if (CollectionUtils.isEmpty(panelSimpleList)) {
            return Collections.emptyList();
        }
        List<Container> result = new ArrayList<>(panelSimpleList.size());
        for (PanelSimpleInfo panel : panelSimpleList) {
            result.add(getEachPanelContent(panel, queryModel));
        }
        return result;
    }

    private Container getEachPanelContent(PanelSimpleInfo panel, PanelQueryModel queryModel) {
        Integer panelVersion = null;
        if (PolicyValidator.isTestPolicy(queryModel.getPolicyCategory()) && ResourceStatusValidator.isToAuditing(panel.getDuplicateStatus())) {
            panelVersion = panel.getDuplicateVersion();
        }
        PanelContentInfo panelContentInfo;
        if (PanelTypeConstants.isGroup(panel.getPanelType())) {
            //目前分组板块仅获取第一个分组
            //return getEachGroupPanelFirstContent(panel, queryModel);
            panelContentInfo = panelInfoCache.getGroupPanelFirstContent(panel.getPanelId(), panelVersion, queryModel.getPolicyCategory());
            handleDmpBlocks(panelContentInfo, queryModel, true);
        } else {
            //return getEachNormalPanelContent(panel, queryModel);
            panelContentInfo = panelInfoCache.getPanelContent(panel.getPanelId(), panelVersion, queryModel.getPolicyCategory());
            handleDmpBlocks(panelContentInfo, queryModel, false);
        }
        return TabContentResultUtil.buildPanelContainer(panelContentInfo);
    }

    private Container getEachNormalPanelContent(PanelSimpleInfo panel, PanelQueryModel queryModel) {
        Integer panelVersion = null;
        if (PolicyValidator.isTestPolicy(queryModel.getPolicyCategory()) && ResourceStatusValidator.isToAuditing(panel.getDuplicateStatus())) {
            panelVersion = panel.getDuplicateVersion();
        }
        PanelContentInfo panelContentInfo = panelInfoCache.getPanelContent(panel.getPanelId(), panelVersion, queryModel.getPolicyCategory());
        handleDmpBlocks(panelContentInfo, queryModel, false);
        return TabContentResultUtil.buildPanelContainer(panelContentInfo);
    }

    /**
     * 获取分组板块第一个分组
     * @return
     */
    private Container getEachGroupPanelFirstContent(PanelSimpleInfo panel, PanelQueryModel queryModel) {
        Integer panelVersion = null;
        if (PolicyValidator.isTestPolicy(queryModel.getPolicyCategory()) && ResourceStatusValidator.isToAuditing(panel.getDuplicateStatus())) {
            panelVersion = panel.getDuplicateVersion();
        }
        PanelContentInfo panelContentInfo = panelInfoCache.getGroupPanelFirstContent(panel.getPanelId(), panelVersion, queryModel.getPolicyCategory());
        handleDmpBlocks(panelContentInfo, queryModel, true);
        return TabContentResultUtil.buildPanelContainer(panelContentInfo);
    }

    private void handleDmpBlocks(PanelContentInfo panelContentInfo, PanelQueryModel queryModel, boolean groupPanel) {
        dmpTabPanelService.handleBlockDmp(panelContentInfo, queryModel ,groupPanel);
    }
}
